<template>
  <div class='container'>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="所属学科" prop="subjectName">
            <el-select v-model="ruleForm.subjectName" placeholder="请选择">
              <el-option v-for="item in tableData1" :key="item.id" :label="item.subjectName" :value="item.subjectID"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="directoryName">
          <el-input  v-model="ruleForm.directoryName" placeholder="请输入目录名称"></el-input>
        </el-form-item>
        <div class="btn">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="subupda">确 定</el-button>
        </div>
    </el-form>

  </div>
</template>

<script>
import { list, add, update } from '@/api/hmmm/directorys'
export default {
  props: {
    dialogVisible: {
      type: Boolean
    },
    row: {
      type: Object,
      default: () => {}
    },
    isedit: {
      type: Boolean
    }
  },
  data () {
    return {
      ruleForm: { directoryName: '', subjectName: '' },
      rules: {
        directoryName: [{ required: true, message: '请输入目录名称', trigger: 'blur' }],
        subjectName: [{ required: true, message: '请选择', trigger: 'change' }]
      },
      obj: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      obj1: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      tableData: [],
      tableData1: []
    }
  },
  // 监听isedit
  watch: {
    isedit: {
      handler (val) {
        if (val) {
          this.ruleForm = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true
    }
  },
  methods: {
    // 点击取消按钮
    cancel () {
      this.$emit('update:dialogVisible', false)
    },
    // 获取数据
    async getupdate () {
      // 展示当前页面
      const res = await list(this.obj)
      this.tableData = res.data.items
      // 新的下拉栏
      this.obj1.pagesize = res.data.counts
      const res2 = await list(this.obj1)
      this.tableData1 = res2.data.items
      // console.log(this.tableData, 4546000)
    },
    // 点击确认按钮
    subupda () {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return
        this.isedit ? this.edit() : this.addfn()
        this.success()
      })
    },
    // 添加时
    async addfn () {
      // console.log(this.ruleForm, 5659)
      await add({ subjectID: this.ruleForm.subjectName, directoryName: this.ruleForm.directoryName })
    },
    // 修改时
    async edit () {
      await update(this.ruleForm, { subjectID: this.ruleForm.subjectName, directoryName: this.ruleForm.directoryName })
    },
    // 刷新页面和关闭弹框
    success () {
      this.$emit('success')
    }
  },
  created () {
    this.getupdate()
  }
}
</script>

<style scoped lang='scss'>
.btn{
  text-align:right;
}
.el-input{
  width: 100%;
}
.el-select--medium{
  width: 100%;
}
</style>
